package com.yssoft.service.core;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TDepartment;
import com.yssoft.mapper.core.TDepartmentMapper;
import com.yssoft.shiro.MyRealm;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 陈翼东
 * @time 2018/4/25 下午 07:34
 * @ 部门信息
 */
@Service
public class DepartmentService {
    
    @Resource
    private TDepartmentMapper departmentMapper;
    
    /**
     * 根据部门id查询部门信息
     * @param departmentId 部门id
     * @return 部门对象
     */
    public TDepartment queryTDepartmentById(Integer departmentId){
        return departmentMapper.selectByPrimaryKey(departmentId);
    }
    
    /**
     * 分页查询部门信息
     * @param page
     * @param limit
     * @param name
     * @param code
     * @return
     */
    public JSONObject queryDepartmentPage(int page, int limit, String name, String code) {
        Integer companyId = UserHelper.getCurrentCompanyId();//当前登陆人企业
        PageHelper.startPage(page,limit);
        Map<String,Object> params=new HashMap<>();
        params.put("companyId" , companyId);
        params.put("name",name);
        params.put("code",code);
        List<TDepartment> pagelist = departmentMapper.queryDepartmentPage(params);
        int pageCount = departmentMapper.queryDepartmentCount(params);
        try {
            return LayuiUtil.getJSONObjectByObject(pagelist,pageCount,"","");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 查询我的企业的部门Tree使用
     * @return
     */
    public List<Map<String,Object>> queryMyDeptTree() {
        Integer myCompany = UserHelper.getCurrentCompanyId();
        List<Map<String,Object>> deptList=null;
        if(myCompany!=null) {
            deptList=  departmentMapper.queryMyDeptByCompanyNUll(myCompany + "");
        }else{
            deptList= departmentMapper.queryMyDeptNull();
        }
        Iterator<Map<String,Object>> it = deptList.iterator();
        List<Integer> genList=new ArrayList<>();
        while (it.hasNext()) {
            Map<String, Object> map1 = it.next();
            if (map1.get("pid") == null) {
                genList.add(Integer.parseInt(map1.get("id") + ""));
                for (Map<String, Object> map2 : deptList) {
                    if (map2.get("pid")!=null && map2.get("pid").equals(map1.get("id"))) {
                        List<Map<String, Object>> childrens = (List<Map<String, Object>>) map1.get("children");
                        if (childrens == null) {
                            childrens = new ArrayList<>();
                        }
                        childrens.add(map2);
                        map1.put("children", childrens);
                    }
                }
            }else {
                for (Map<String, Object> map2 : deptList) {
                    if (map2.get("id").equals(map1.get("pid")) && map2.get("pid") != null) {
                        List<Map<String, Object>> childrens = (List<Map<String, Object>>) map2.get("children");
                        if (childrens == null) {
                            childrens = new ArrayList<>();
                        }
                        childrens.add(map1);
                        map2.put("children", childrens);
                    }
                }
            }
        }
        Iterator<Map<String,Object>> it2 = deptList.iterator();
        while (it2.hasNext()) {
            Map<String,Object> map = it2.next();
            if(map.get("pid")!=null){
                it2.remove();
            }
        }
        return deptList;
    }
    
    /**
     * 查询我的企业的部门tableTree使用
     * @return
     */
    public List<Map<String,Object>> queryMyDeptTableTree() {
        Integer myCompany = UserHelper.getCurrentCompanyId();
      
        if(myCompany!=null) {
            return  departmentMapper.queryMyDeptByCompany(myCompany + "");
        }else{
            return departmentMapper.queryMyDept();
        }
        
    }
    
    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteDepartment(String id) {
        List<TDepartment> childDept = departmentMapper.queryDeptChildren(id);
        List<Integer> idList=new ArrayList<>();
        for (TDepartment department : childDept) {
            idList.add(department.getId());
        }
       return  departmentMapper.deleteDepartment(idList);
    }
    
    /**
     * 添加部门信息
     * @param department
     */
    @Transactional(rollbackFor = Exception.class)
    public void addDepartment(TDepartment department) {
        department.setCreateTime(new Date());
        MyRealm.ShiroUser user = UserHelper.getCurrentUser();
        department.setCreatorId(user.getPersonId());
        department.setCreatorName(user.getPersonName());
        department.settEId(user.getEntId());
        department.setDeleteFlag("0");
        departmentMapper.insertSelective(department);
    }
    
    /**
     * 修改部门信息
     * @param department
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDepartment(TDepartment department) {
        departmentMapper.updateById(department);
    }
}
